package com.oneplatform.organisation.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesuite.common.JeesuiteBaseException;
import com.jeesuite.common.util.BeanUtils;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.mybatis.plugin.pagination.PageExecutor;
import com.jeesuite.mybatis.plugin.pagination.PageExecutor.PageDataLoader;
import com.jeesuite.mybatis.plugin.pagination.PageParams;
import com.oneplatform.base.exception.AssertUtil;
import com.oneplatform.base.exception.ExceptionCode;
import com.oneplatform.base.model.PageResult;
import com.oneplatform.organisation.dao.entity.CompanyEntity;
import com.oneplatform.organisation.dao.entity.DepartmentEntity;
import com.oneplatform.organisation.dao.entity.EmployeeDepartmentEntity;
import com.oneplatform.organisation.dao.entity.EmployeeEntity;
import com.oneplatform.organisation.dao.entity.PositionEntity;
import com.oneplatform.organisation.dao.mapper.CompanyEntityMapper;
import com.oneplatform.organisation.dao.mapper.DepartmentEntityMapper;
import com.oneplatform.organisation.dao.mapper.EmployeeDepartmentEntityMapper;
import com.oneplatform.organisation.dao.mapper.EmployeeEntityMapper;
import com.oneplatform.organisation.dao.mapper.PositionEntityMapper;

/**
 * generated by www.jeesuite.com
 */
@Service
public class EmployeeService {

	private @Autowired EmployeeEntityMapper employeeMapper;
	private @Autowired EmployeeDepartmentEntityMapper employeeDepartmentMapper;
	private @Autowired CompanyEntityMapper companyMapper;
	private @Autowired DepartmentEntityMapper departmentMapper;
	private @Autowired PositionEntityMapper positionsMapper;

	@Transactional
	public void addEmployee(EmployeeEntity entity,Integer departmentId,Integer positionId) {
		
		DepartmentEntity departmentEntity = departmentMapper.selectByPrimaryKey(departmentId);
		entity.setCompanyId(departmentEntity.getCompanyId());
		employeeMapper.insertSelective(entity);
		EmployeeDepartmentEntity ed = new EmployeeDepartmentEntity();
		ed.setEmployeeId(entity.getId());
		ed.setDepartmentId(departmentId);
		ed.setInActive(true);
		PositionEntity positionEntity = positionsMapper.selectByPrimaryKey(positionId);
		ed.setPositonName(positionEntity.getName());
		ed.setCreatedAt(entity.getCreatedAt());
		ed.setCreatedBy(entity.getCreatedBy());
		employeeDepartmentMapper.insertSelective(ed);
	}

	public void updateEmployee(EmployeeEntity entity) {
		EmployeeEntity originEntity = employeeMapper.selectByPrimaryKey(entity.getId());
		AssertUtil.notNull(originEntity);
		BeanUtils.copy(entity, originEntity);
		employeeMapper.updateByPrimaryKey(originEntity);
	}
	
	public void deleteEmployee(Integer id) {
		EmployeeEntity entity = employeeMapper.selectByPrimaryKey(id);
		AssertUtil.notNull(entity);
		if(entity.getInActive())throw new JeesuiteBaseException(ExceptionCode.OPTER_NOT_ALLOW.code, "未离职员工不能删除");
		employeeMapper.deleteByPrimaryKey(id);
	}
	
	public EmployeeEntity findEmployeeById(Integer id){
		EmployeeEntity entity = employeeMapper.selectByPrimaryKey(id);
		AssertUtil.notNull(entity);
		return entity;
	}
	
    public PageResult<EmployeeEntity> pageQuery(PageParams pageParam,Map<String, Object> conditions){
		Page<EmployeeEntity> page = PageExecutor.pagination(pageParam, new PageDataLoader<EmployeeEntity>() {
			@Override
			public List<EmployeeEntity> load() {
				return employeeMapper.findByParam(conditions);
			}
		});
		
		for (EmployeeEntity entity : page.getData()) {
			buildViewDatas(entity);
		}
		
		return new PageResult<EmployeeEntity>(page.getPageNo(), page.getPageSize(), page.getTotal(), page.getData());
	}


	private void buildViewDatas(EmployeeEntity entity) {
		CompanyEntity companyEntity = companyMapper.selectByPrimaryKey(entity.getCompanyId());
		entity.setCompanyName(companyEntity.getName());
		List<EmployeeDepartmentEntity> departmentRalates = employeeDepartmentMapper.findActiveByEmployeeId(entity.getId());
		for (EmployeeDepartmentEntity e : departmentRalates) {
			String departInfo = departmentMapper.selectByPrimaryKey(e.getDepartmentId()).getName() + "(" + e.getPositonName() + ")";
			entity.setDepartmentInfo(departInfo);
			break;//多个部门任职先不考虑
		}
	}

}
